﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Alphacat
{
    class HttpUtils
    {
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static CookieContainer getCookies(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
                request.CookieContainer = new CookieContainer();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Close();
            }
            catch (Exception)
            {
                //MsgBox mb = new MsgBox("系统信息", "网络连接错误！");
                // mb.ShowDialog();
            }
            return request.CookieContainer;
        }
        /// <summary>
        /// http Get 请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static string httpGet(string url, CookieContainer cookies)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            string res = "";
            try
            {
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
                request.CookieContainer = cookies;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (Exception)
            {
                // MsgBox mb = new MsgBox("系统信息", "网络请求错误！");
                //  mb.ShowDialog();
            }
            return res;
        }

        public static object httpGetReturnCookies(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            string res = "";
            try
            {
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
                request.CookieContainer = new CookieContainer(); ;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (Exception)
            {
                // MsgBox mb = new MsgBox("系统信息", "网络请求错误！");
                //  mb.ShowDialog();
            }

            return new object[] { res, request.CookieContainer};
        }

        /// <summary>
        /// Http Post 请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static Stream getImg(string url, CookieContainer cookies)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = null;
            try
            {
                request.Method = "GET";
                request.Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
                request.CookieContainer = cookies;
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception)
            {
                //  MsgBox mb = new MsgBox("系统信息", "网络请求错误！");
                //  mb.ShowDialog();
            }
            return response.GetResponseStream();
        }

        /// <summary>
        /// Http Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="postCookies"></param>
        /// <returns></returns>
        public static string httpPost(string url, string postData, CookieContainer cookies)
        {

            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
            string res = "";
            try
            {
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.Accept = "text/html, application/xhtml+xml, */*";
                webReq.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
                webReq.CookieContainer = cookies;

                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                webReq.ContentLength = byteArray.Length;

                Stream requestStream = webReq.GetRequestStream();
                requestStream.Write(byteArray, 0, byteArray.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (Exception)
            {
                // MsgBox mb = new MsgBox("系统信息", "网络请求错误！");
                //mb.ShowDialog();
            }

            return res;
        }

        

    }
}
